package by.amakarevich.webapp.page.booking.panel;

import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import by.amakarevich.datamodel.Booking;
import by.amakarevich.datamodel.Booking_;
import by.amakarevich.datamodel.UserAccount;
import by.amakarevich.services.BookingService;
import by.amakarevich.services.UserAccountService;
import by.amakarevich.webapp.page.MyPagingNavigator;
import by.amakarevich.webapp.page.booking.BookingPage;
import by.amakarevich.webapp.page.booking.info.BookingInfoPage;

public class BookingListPanel extends Panel {

	@Inject
	private BookingService bookingService;
	@Inject
	private UserAccountService userAccountService;

	public BookingListPanel(String id) {
		super(id);
		BookingDataProvider bookingDataProvider = new BookingDataProvider();

		final WebMarkupContainer tableBody = new WebMarkupContainer("wrapper-body") {
			@Override
			protected void onBeforeRender() {
				super.onBeforeRender();
				add(new AttributeModifier("title", new Model(System.currentTimeMillis())));
			}
		};

		tableBody.add(new AttributeModifier("title", new Model(System.currentTimeMillis())));
		add(tableBody);

		DataView<Booking> dataView = new DataView<Booking>("list", bookingDataProvider, 15) {
			@Override
			protected void populateItem(Item<Booking> item) {
				final Booking booking = item.getModelObject();
				item.add(new Label("id"));
				item.add(new Label("received"));
				item.add(new Label("deadlines"));
				item.add(new CheckBox("status").setEnabled(false));
				item.add(new CheckBox("payStatus").setEnabled(false));
				item.add(new CheckBox("canceled").setEnabled(false));
				item.add(new Label("totalPrice"));

				item.add(new Link("status-update-ajax") {
					@Override
					public void onClick() {
						if(booking.getStatus()==false){
								booking.setStatus(true);
							}else{
								booking.setStatus(false);
							}
						bookingService.updateBooking(booking);
						setResponsePage(new BookingPage());
					}

				});
				
				item.add(new Link("user-to-black-list-link") {
					Booking tempBooking = bookingService.getByBookingId(booking.getId());
					UserAccount user= tempBooking.getUserAccount();
					@Override
					protected void onConfigure() {
						setVisible(!user.getInBlackList());
					}
					@Override
					public void onClick() {
						user.setInBlackList(true);
						userAccountService.saveOrUpdate(user);
						setResponsePage(new BookingPage());
					}
				});
				
				item.add(new Link("payStatus-update-ajax") {
					@Override
					protected void onConfigure() {
						setVisible(booking.getStatus());
					}
					@Override
					public void onClick() {
						if(booking.getPayStatus()==false){
							booking.setPayStatus(true);
						}else{
							booking.setPayStatus(false);
						}
						bookingService.updateBooking(booking);
						setResponsePage(new BookingPage());
					}

				});
				
				item.add(new Link("canceled-update-ajax") {
					@Override
					protected void onConfigure() {
						setVisible(!booking.getPayStatus());
					}
					@Override
					public void onClick() {
						if(booking.getCanceled()==false){
							booking.setCanceled(true);
						}else{
							booking.setCanceled(false);
						}
						bookingService.updateBooking(booking);
						setResponsePage(new BookingPage());
					}

				});
				
				item.add(new Link("booking-info-page-link") {
					@Override
					public void onClick() {
						setResponsePage(new BookingInfoPage(booking));
					}
				});

			}
		};

		tableBody.add(dataView);

		add(new MyPagingNavigator("paging", dataView));

		add(new OrderByBorder<SingularAttribute<Booking, ?>>("sortByReceived", Booking_.received, bookingDataProvider));
		add(new OrderByBorder<SingularAttribute<Booking, ?>>("sortByDeadlines", Booking_.deadlines, bookingDataProvider));
		add(new OrderByBorder<SingularAttribute<Booking, ?>>("sortByStatus", Booking_.status, bookingDataProvider));
		add(new OrderByBorder<SingularAttribute<Booking, ?>>("sortByPayStatus", Booking_.payStatus, bookingDataProvider));
		add(new OrderByBorder<SingularAttribute<Booking, ?>>("sortByCanceled", Booking_.canceled, bookingDataProvider));
		add(new OrderByBorder<SingularAttribute<Booking, ?>>("sortByTotalPrice", Booking_.totalPrice, bookingDataProvider));
		

	
	}
	private class BookingDataProvider extends SortableDataProvider<Booking, SingularAttribute<Booking, ?>> {

		public BookingDataProvider() {
			super();
			setSort(Booking_.id, SortOrder.ASCENDING);
		}

		@Override
		public Iterator<? extends Booking> iterator(long first, long count) {

			SingularAttribute<Booking, ?> sortParam = getSort().getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);
			return bookingService.getSortedAllBooking(sortParam, ascending, (int) first, (int) count).iterator();
		}

		@Override
		public long size() {
			return bookingService.getCount();
		}

		public IModel<Booking> model(Booking booking) {
			return new CompoundPropertyModel<Booking>(booking);
		}
	}

}
